package com.xinya.wanandroidclient.ui.home

import android.os.Looper
import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.xinya.wanandroidclient.base.BaseRepository
import com.xinya.wanandroidclient.bean.HomeArticleBean
import com.xinya.wanandroidclient.bean.HomeBannerBean
import com.xinya.wanandroidclient.http.ApiException
import com.xinya.wanandroidclient.http.LoadState
import com.xinya.wanandroidclient.http.RetrofitManager
import com.xinya.wanandroidclient.http.WanAndroidApiService
import kotlinx.coroutines.CoroutineScope

/**
 *
 * @ClassName:      HomeRepo$
 * @Description:    java类作用描述
 * @Author:         xinya
 * @CreateDate:     2021/2/24$ 14:58$
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
class HomeRepo(coroutineScope: CoroutineScope) :
    BaseRepository(coroutineScope) {

    companion object {
        private const val TAG = "HomeRepo"
    }

    /**
     * 获取首页Banner数据
     */
    fun getBanner(
        banner: MutableLiveData<MutableList<HomeBannerBean>>
    ) {
        launch(
            block = {
                RetrofitManager.getApiService(WanAndroidApiService::class.java)
                    .getHomeBanner()
                    .result()
            },
            success = {
                banner.postValue(it)
            }
        )
    }

    /**
     * 获取首页文章
     */
    fun getArticle(
        page: Int, articleData: MutableLiveData<HomeArticleBean>,
        refreshStateLiveData: MutableLiveData<LoadState>
    ) {
        launch(
            block = {
                refreshStateLiveData.postValue(LoadState.Loading)
                RetrofitManager.getApiService(WanAndroidApiService::class.java)
                    .getHomeArticle(page)
                    .result()
            },
            success = {
                refreshStateLiveData.postValue(LoadState.Success)
                articleData.postValue(it)
            },
            error = {
                refreshStateLiveData.postValue(LoadState.Error)
            }
        )
    }

    /**
     * 获取置顶文章
     */
    fun getArticleTop(
        articleTopLiveData: MutableLiveData<MutableList<HomeArticleBean.Data>>,
        refreshStateLiveData: MutableLiveData<LoadState>
    ) {
        launch(
            block = {
                refreshStateLiveData.postValue(LoadState.Loading)
                RetrofitManager.getApiService(WanAndroidApiService::class.java)
                    .getHomeTopArticle()
                    .result()
            },
            success = {
                refreshStateLiveData.postValue(LoadState.Success)
                articleTopLiveData.postValue(it)
            },

            error = {
                refreshStateLiveData.postValue(LoadState.Error)
            })
    }
}